// JavaScript Document
;(function($){
	$.fn.paging=function(obj){
		var parameter={
			"container":".pagedv",
			"indexmax":5,
			"indexcur":1,
			"flag":true,
			"clickevent":function(flag, indexcur){
				if(flag){
					return indexcur;
				}
      		},
		}
		var pageExpand=$.extend(parameter, obj);
		
		var u1li="";
		var element="<a class='page_first' href='javascript:;'>首页</a><a class='page_prev' href='javascript:;'>上一页</a><div class='paging_dv'><ul class='paging'></ul></div><a class='page_next' href='javascript:;'>下一页</a><a class='page_last' href='javascript:;'>末页</a><div class='page_dvright'><span>当前第<span class='page_cur'>"+pageExpand.indexcur+"</span>页/共<span class='page_all'>"+pageExpand.indexmax+"</span>页</span><span>&nbsp;&nbsp;转到<input type='text' class='paging_cur' value='"+(pageExpand.indexcur+1)+"'>页</span><a class='paging_curbtn' href='javascript:;'>确认</a></div>";
		$(pageExpand.container+" .page_dv").append(element);	
		for(var i=1; i<=pageExpand.indexmax; i++){
			u1li+="<li><a href='javascript:;'>"+i+"</a></li>";		
		}
		$(pageExpand.container+" .paging").append(u1li).css({"width":40*pageExpand.indexmax+"px"});
		$(pageExpand.container+" .paging li:eq(0) a").addClass("cur");
		if(pageExpand.indexcur==pageExpand.indexmax){
			$(pageExpand.container+" .paging_cur").val(pageExpand.indexcur);
		}
		//移动效果
		function numberState(){
			if(pageExpand.indexmax>5){
				if(pageExpand.indexmax-pageExpand.indexcur>=5){
					$(pageExpand.container+" .paging li").children("a").removeClass("cur");
					$(pageExpand.container+" .paging li:eq("+(pageExpand.indexcur-1)+")").children("a").addClass("cur");
					$(pageExpand.container+" .paging").css({"transition":"all 0.3s","left":-40*(pageExpand.indexcur-1)+"px"});
				}else{
					$(pageExpand.container+" .paging li").children("a").removeClass("cur");
					$(pageExpand.container+" .paging li:eq("+(pageExpand.indexcur-1)+")").children("a").addClass("cur");
					$(pageExpand.container+" .paging").css({"transition":"all 0.3s","left":-40*(pageExpand.indexmax-5)+"px"});
				}
			}else{
				$(pageExpand.container+" .paging").css({"transition":"all 0.3s","left":"0px"});
				$(pageExpand.container+" .paging li").children("a").removeClass("cur");
				$(pageExpand.container+" .paging li:eq("+(pageExpand.indexcur-1)+")").children("a").addClass("cur");
			}
			//当前页
			$(pageExpand.container+" .page_cur").text(pageExpand.indexcur);
			//跳转页
			if(pageExpand.indexcur==pageExpand.indexmax){
				$(pageExpand.container+" .paging_cur").val(pageExpand.indexcur);
			}else{
				$(pageExpand.container+" .paging_cur").val((pageExpand.indexcur+1));
			}
			pageExpand.clickevent(pageExpand.flag, pageExpand.indexcur);
		}
		
		
		//上一页按钮
		$(pageExpand.container+" .page_prev").on("click", function(){
			if(pageExpand.indexcur==1){
				pageExpand.flag=false;
			}else{
				pageExpand.flag=true;
			}
			pageExpand.indexcur--;
			if(pageExpand.indexcur<=1){
				pageExpand.indexcur=1;
			}
			numberState();		
		});
		
		//下一页按钮
		$(pageExpand.container+" .page_next").on("click", function(){
			if(pageExpand.indexcur==pageExpand.indexmax){
				pageExpand.flag=false;
			}else{
				pageExpand.flag=true;
			}
			pageExpand.indexcur++;
			if(pageExpand.indexcur>=pageExpand.indexmax){
				pageExpand.indexcur=pageExpand.indexmax;
			}
			numberState();
		});
		
		//随机点击
		$(pageExpand.container+" .paging li").each(function(n){
			$(this).click(function(){			
				if(n+1==pageExpand.indexcur){
					pageExpand.flag=false;
				}else{
					pageExpand.flag=true;
				}
				pageExpand.indexcur=n+1;	
				numberState();
			})
		});	
		
		//首页
		$(pageExpand.container+" .page_first").on("click", function(){
			if(pageExpand.indexcur!=1){
				pageExpand.flag=true;
				pageExpand.indexcur=1;
				numberState();
			}else{
				pageExpand.flag=false;
				pageExpand.indexcur=1;
				numberState();
			}
		});
		
		//末页
		$(pageExpand.container+" .page_last").on("click", function(){
			if(pageExpand.indexcur!=pageExpand.indexmax){
				pageExpand.flag=true;
				pageExpand.indexcur=pageExpand.indexmax;
				numberState();
			}else{
				pageExpand.flag=false;
				pageExpand.indexcur=pageExpand.indexmax;
				numberState();
			}
		});
		
		//输入框输入
		var inputvalue=pageExpand.indexcur;
		$(pageExpand.container+" .paging_cur").on("keyup keydown", function(){
			inputvalue=parseInt($(this).val());
			if(inputvalue>pageExpand.indexmax){
				inputvalue=pageExpand.indexmax;
			}
			if(inputvalue>0){
				$(this).val(inputvalue);
			}else if(inputvalue<=0){
				$(this).val(pageExpand.indexcur);
			}
		});
		
		//输入框确认跳转
		$(pageExpand.container+" .paging_curbtn").on("click", function(){
			var inputvalue=parseInt($(pageExpand.container+" .paging_cur").val());
			pageExpand.flag=true;
			pageExpand.indexcur=inputvalue;
			numberState();
		});
	}
})(jQuery);